home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 October: Mac OS SDK / Dev.CD Oct 97 SDK1.toast / Development Kits (Disc 1) / Apple Shared Library Manager / ASLM Examples / Sample Apps / CCPlusSample / Sources / SampleLibrary.cp < prev    next >
Encoding:
Text File  |  1996-11-19  |  12.1 KB  |  390 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        SampleLibrary.cp
  3.  
  4.     Contains:    Implementation of the CCPlusSampleLibrary shared library.
  5.  
  6.     Copyright:    © 1993-1994 by Apple Computer, Inc., all rights reserved.
  7.  
  8. */
  9.  
  10.  
  11. #include <GlobalNew.h>
  12. #include <LibraryManager.h>
  13. #include <LibraryManagerClasses.h>
  14. #include <LibraryManagerUtilities.h>
  15.  
  16. #include <String.h>
  17. #include <Events.h>
  18. #include <QuickDraw.h>
  19. #include <Resources.h>
  20. #include <Menus.h>
  21. #include <Windows.h>
  22. #include <StdIO.h>
  23.  
  24. #include "SampleLibrary.h"
  25.  
  26. /*————————————————————————————————————————————————————————————————————————————————————
  27.     class TTrafficLight
  28.     
  29.     Since we aren't exporting this class (we could if we wanted to), we put its
  30.     interface here rather than in SampleLibrary.h, which clients of the library
  31.     get to see.
  32. ————————————————————————————————————————————————————————————————————————————————————*/
  33.  
  34. class TTrafficLight : public TDynamic {
  35.     public:
  36.                             TTrafficLight();
  37.         virtual                ~TTrafficLight();
  38.         
  39.         // public virtual methods
  40.         
  41.         virtual Boolean        IsValid() const;    // Override
  42.  
  43.         virtual Boolean        GetLightState() const;    // returns the state of the light
  44.         virtual void         SetLightState( Boolean state ); // set it to new state
  45.         virtual void        DrawLight();        // displays the light
  46.         virtual void        AdjustMenus( Boolean update );
  47.         virtual void        DoMenuCommand( short menuItem );
  48.  
  49.     protected:
  50.         static Boolean        GoGetRect( short rectID, Rect *theRect );
  51.  
  52.     private:                        
  53.         Boolean                fLightState;        // 0:indicates OFF, 1:indicates ON
  54.         Rect                fStopRect;            // rectangle for stop light
  55.         Rect                fGoRect;            // rectangle for go rectangle
  56.         WindowPtr            fWindow;            // objects grafport and window
  57. };
  58.  
  59.  
  60. //————————————————————————————————————————————————————————————————————————————————————
  61. //    TTrafficLight
  62. //    
  63. //    TrafficLight constructor, allocates window record from client's pool, and ini-
  64. //    tialize our light rectangles.
  65. //————————————————————————————————————————————————————————————————————————————————————
  66.  
  67. TTrafficLight::TTrafficLight()
  68. {
  69.     long     savedrefnum = -1;
  70.     Ptr     wstorage = nil;
  71.     
  72.     VOLATILE(wstorage);                // use this if variable is being modified within
  73.     VOLATILE(savedrefnum);            // TRY and accessed in the CATCH_ALL.
  74.  
  75.     fWindow = nil;
  76.     
  77.     Trace("TTrafficLight Constructor\n");    // send the output to trace monitor's window
  78.  
  79.     TRY
  80.         fLightState = false;
  81.         
  82.         wstorage = (Ptr)new WindowRecord;// allocate our storage
  83.     
  84.         FailNULL( wstorage, ErrorCode(), "sorry not enought memory in pool");
  85.                   
  86.         // include the library's file in resource chain to obtain our menus & windows
  87.  
  88.         Fail(GetLocalLibraryFile()->Preflight(savedrefnum), "Failed to preflight");
  89.         
  90.         fWindow = GetNewWindow( rWindow, wstorage, (WindowPtr) -1 );
  91.  
  92.         FailNULL( fWindow, ResError(), "sorry unable to create a window");
  93.     
  94.         TTrafficLight::GoGetRect(rStopRect, &fStopRect); // get rectangle for Stop light
  95.         TTrafficLight::GoGetRect(rGoRect, &fGoRect); // get rectangle for Go light
  96.     
  97.         // Any MENU resources in our shared library is appended to our application
  98.     
  99.         MenuHandle themenu = GetMenu( mLight );    // get our traffic light menu
  100.         InsertMenu( themenu, 0 );        // append to the application's menu
  101.         
  102.         DrawMenuBar();                    // go ahead and update the menu bar
  103.     CATCH_ALL                            // oh no! something failed lets cleanup
  104.         delete wstorage;                // remove the storage
  105.         delete fWindow;                    // free the pool
  106.     ENDTRY
  107.  
  108.     if (savedrefnum != -1)
  109.         GetLocalLibraryFile()->Postflight(savedrefnum); // restore the resource chain
  110. }
  111.  
  112. //————————————————————————————————————————————————————————————————————————————————————
  113. //    ~TTrafficLight
  114. //    
  115. //    our traffic light destructor, delete the window, and unregister the object for
  116. //    inspector
  117. //————————————————————————————————————————————————————————————————————————————————————
  118.  
  119. TTrafficLight::~TTrafficLight()
  120. {
  121.     Trace("TTrafficLight Destructor\n");// tell Trace Monitor where we are
  122.     
  123.     CloseWindow( fWindow );                // close the window
  124.     delete fWindow;                        // we are done, release the memory
  125. }
  126.  
  127. //————————————————————————————————————————————————————————————————————————————————————
  128. //    IsValid
  129. //    
  130. //    returns true if the object was initialized properly after it was created. In our
  131. //  case if window has been allocated, we return true.
  132. //————————————————————————————————————————————————————————————————————————————————————
  133.  
  134. Boolean TTrafficLight::IsValid() const
  135. {
  136.     return( fWindow != nil );            // return true if window has been allocated
  137. }
  138.  
  139. //————————————————————————————————————————————————————————————————————————————————————
  140. //    GetLightState
  141. //    
  142. //    return current state of traffic light
  143. //————————————————————————————————————————————————————————————————————————————————————
  144.  
  145. Boolean TTrafficLight::GetLightState() const
  146. {
  147.     Trace("TTrafficLight::GetLightState\n");// tell Trace Monitor where we are 
  148.     return fLightState;                    // return the state of Traffic Light
  149. }
  150.  
  151. //————————————————————————————————————————————————————————————————————————————————————
  152. //    SetLightState
  153. //    
  154. //    set the light to the new state
  155. //————————————————————————————————————————————————————————————————————————————————————
  156.  
  157. void TTrafficLight::SetLightState( Boolean newState )
  158. {
  159.     Trace("TTrafficLight::SetLightState\n");// tell Trace Monitor what we are doing
  160.     fLightState = newState;                // set the new state
  161.     
  162.     SetPort(fWindow);                    // make sure we are at the right port before
  163.     InvalRect(&fWindow->portRect);        // invalidating the update region
  164. }
  165.  
  166. //————————————————————————————————————————————————————————————————————————————————————
  167. //    DrawLight
  168. //    
  169. //    draw the actual traffic light
  170. //————————————————————————————————————————————————————————————————————————————————————
  171.  
  172. void TTrafficLight::DrawLight()
  173. {
  174.     GrafPtr        oldport;
  175.     
  176.     Trace("TTrafficLight::DrawLight\n");// tell the Trace Monitor what we are doing
  177.  
  178.     if( fWindow ) {
  179.     
  180.         GetPort( &oldport );            // save the old port
  181.         SetPort( fWindow );                // set the new port to our object's port
  182.                 
  183.         EraseRect(&fWindow->portRect);    // clear out any garbage that may linger
  184.         if ( fLightState )                // draw a red (or white) stop light
  185.             ForeColor(redColor);
  186.         else
  187.             ForeColor(whiteColor);
  188.         
  189.         PenSize( 2, 2 );                
  190.         PaintOval(&fStopRect);            
  191.         ForeColor(blackColor);
  192.         FrameOval(&fStopRect);
  193.         if ( ! fLightState )            // draw a green (or white) go light
  194.             ForeColor(greenColor);
  195.         else
  196.             ForeColor(whiteColor);
  197.         PaintOval(&fGoRect);
  198.         ForeColor(blackColor);
  199.         FrameOval(&fGoRect);
  200.         PenSize( 1, 1 );
  201.  
  202.         SetPort( oldport );                // restore the grafpointer
  203.     }
  204. }
  205.  
  206. //————————————————————————————————————————————————————————————————————————————————————
  207. //    AdjustMenus
  208. //    
  209. //    Enable and disable traffic light's menus based on the current state.
  210. //————————————————————————————————————————————————————————————————————————————————————
  211.  
  212. void TTrafficLight::AdjustMenus( Boolean active )
  213. {
  214.     long            savedrefnum;
  215.     MenuHandle        menu;
  216.     OSErr            err;
  217.     
  218.     Trace("TTrafficLight::AdjustMenus\n");// tell the Trace Monitor where we are
  219.  
  220.                                         // include the library's file in resource chain
  221.     if( err = GetLocalLibraryFile()->Preflight( savedrefnum ) ) {
  222.         Trace("Preflight failed err=%d\n", err);// let Trace Monitor know what failed
  223.         return;
  224.     }
  225.     
  226.     if(  menu = GetMenuHandle(mLight) ) {    // get the handle to traffic light menu
  227.         
  228.         if ( active ) {                    // do we need to enable them?
  229.             EnableItem(menu, iStop);
  230.             EnableItem(menu, iGo);
  231.         } else {
  232.             DisableItem(menu, iStop);
  233.             DisableItem(menu, iGo);
  234.         }
  235.  
  236.         CheckItem(menu, iStop, fLightState); // we can also determine check/uncheck state, too
  237.         CheckItem(menu, iGo, ! fLightState);        
  238.     }
  239.     else
  240.         Trace("GetMenuHandle returned NIL\n");    // let Trace Monitor know we failed
  241.                                             // restore the resource change 
  242.     err = GetLocalLibraryFile()->Postflight(savedrefnum);
  243. }
  244.  
  245. //————————————————————————————————————————————————————————————————————————————————————
  246. //    DoMenuCommand
  247. //    
  248. //    Enable and disable menus based on the current state of traffic light.
  249. //————————————————————————————————————————————————————————————————————————————————————
  250.  
  251. void TTrafficLight::DoMenuCommand( short menuItem )
  252. {
  253.     long            savedrefnum;
  254.     OSErr            err;
  255.  
  256.     Trace("TTrafficLight::DoMenuCommand\n");// tell Trace Monitor what we are doing
  257.                                         // include the library's file in resource chain
  258.     if( err = GetLocalLibraryFile()->Preflight( savedrefnum ) ) {
  259.         Trace("Preflight failed err=%d\n", err);// let Trace Monitor know what failed
  260.         return;
  261.     }
  262.  
  263.     switch ( menuItem ) {                // what menu item selected?
  264.         case iStop:
  265.             SetLightState( true );            // set and update
  266.             break;
  267.         case iGo:
  268.             SetLightState( false );            // set and  update
  269.             break;
  270.     }
  271.                                         // restore the resource change 
  272.     err = GetLocalLibraryFile()->Postflight(savedrefnum);
  273. }
  274.  
  275. //————————————————————————————————————————————————————————————————————————————————————
  276. //    GoGetRect
  277. //    
  278. //    loads the global rectrangles that are used for drawing the traffic lights.
  279. //————————————————————————————————————————————————————————————————————————————————————
  280.     
  281. Boolean TTrafficLight::GoGetRect( short rectID, Rect *theRect)
  282. {
  283.     Handle        resource;
  284.     
  285.     TRY
  286.         resource = GetResource('RECT', rectID);    // get 'RECT' resource from library file
  287.         FailNULL( resource, ResError(), "'RECT' resource is missing" );
  288.         *theRect = **((Rect**) resource);
  289.     CATCH_ALL
  290.         DebugStr("\pFailed to get 'RECT' resource");// inform the user we failed
  291.         SetRect( theRect, 10, 10, 60, 60 );
  292.         return false;
  293.     ENDTRY
  294.     
  295.     return true;
  296. }
  297.  
  298. /*————————————————————————————————————————————————————————————————————————————————————
  299.     NewTrafficLight
  300.     
  301.     creates an instance of our traffic light, and return the pointer to the object.
  302. ————————————————————————————————————————————————————————————————————————————————————*/
  303.  
  304. TTrafficLight    *NewTrafficLight()
  305. {
  306.     TTrafficLight    *trafficlight;
  307.     
  308.     TRY
  309.         trafficlight = new TTrafficLight;// allocate space from default pool
  310.         FailNULL( trafficlight, ErrorCode(), "Failed to create a TTrafficLight object");
  311.         if( ! trafficlight->IsValid() )    {// if we unable to initialize it
  312.             Trace("Failed to initialize TTrafficLight");
  313.             delete trafficlight;        // failed, so deallocate the object
  314.             trafficlight = nil;            // return nil to inform the client
  315.         }
  316.     CATCH_ALL
  317.         Trace("Failed to create an instance of TTrafficLight");
  318.     ENDTRY
  319.     
  320.     return trafficlight;
  321. }
  322.  
  323. /*————————————————————————————————————————————————————————————————————————————————————
  324.     FreeTrafficLight
  325.     
  326.     delete the traffic light object created by NewTrafficLight
  327. ————————————————————————————————————————————————————————————————————————————————————*/
  328.  
  329. void FreeTrafficLight( TTrafficLight *trafficLight )
  330. {
  331.     delete trafficLight;
  332. }
  333.  
  334. /*————————————————————————————————————————————————————————————————————————————————————
  335.     SetLightState
  336.     
  337.     Given a pointer to trafficlight object, set the traffic light to new state.
  338. ————————————————————————————————————————————————————————————————————————————————————*/
  339.  
  340. void SetLightState( TTrafficLight *trafficLight, Boolean state )
  341. {
  342.     trafficLight->SetLightState( state );
  343.  
  344. }
  345.  
  346. /*————————————————————————————————————————————————————————————————————————————————————
  347.     GetLightState
  348.     
  349.     Given a pointer to trafficlight object, get the state of the traffic light.
  350. ————————————————————————————————————————————————————————————————————————————————————*/
  351.  
  352. Boolean GetLightState( TTrafficLight *trafficLight )
  353. {
  354.     return trafficLight->GetLightState();
  355. }
  356.  
  357. /*————————————————————————————————————————————————————————————————————————————————————
  358.     DrawLight
  359.     
  360.     Given a pointer to trafficlight object, draw the traffic light.
  361. ————————————————————————————————————————————————————————————————————————————————————*/
  362.  
  363. void DrawLight( TTrafficLight *trafficLight )
  364. {
  365.     trafficLight->DrawLight();
  366. }
  367.  
  368. /*————————————————————————————————————————————————————————————————————————————————————
  369.     AdjustTrafficLightMenus
  370.     
  371.     Given a pointer to trafficlight object, adjust the menus associated with traffic
  372.     light.
  373. ————————————————————————————————————————————————————————————————————————————————————*/
  374.  
  375. void AdjustTrafficLightMenus( TTrafficLight *trafficLight, Boolean active )
  376. {
  377.     trafficLight->AdjustMenus( active );
  378. }
  379.  
  380. /*————————————————————————————————————————————————————————————————————————————————————
  381.     DoTrafficLightMenuCommand
  382.     
  383.     Given a pointer to trafficlight object, process menu commands associated with our
  384.     traffic light.
  385. ————————————————————————————————————————————————————————————————————————————————————*/
  386.  
  387. void DoTrafficLightMenuCommand( TTrafficLight *trafficLight, short menuItem )
  388. {
  389.     trafficLight->DoMenuCommand( menuItem );
  390. }